<?php
    require_once("condb.php");
    require_once("functions.php");
    
    $output = array();    
    
    $refCode = isset($_GET["refCode"]) ? $_GET["refCode"] : "";
    $brand = isset($_GET["brand"]) ? $_GET["brand"] : "";
    $model = isset($_GET["model"]) ? $_GET["model"] : "";
    $w = isset($_GET["w"]) ? $_GET["w"] : "";
    $h = isset($_GET["h"]) ? $_GET["h"] : "";
    $midp = isset($_GET["midp"]) ? $_GET["midp"] : "";
    
    $log_input = "refCode: " . $refCode . ", Brand: " . $brand . ", Model: " . $model . ", Weight: " . $w . ", Height: " . $h . ", midp: " . $midp;
    if(isset($_GET["refCode"]) && isset($_GET["brand"]) && isset($_GET["model"]) && isset($_GET["w"]) && isset($_GET["h"]) && isset($_GET["midp"])){
        
        $data = array();         
        $data["refCode"] = cleanQuery($_GET["refCode"]);
        
        /* Lấy userId */
        $user = getUser($data["refCode"]);
        $data["userId"] = $user["id"];
        
        $data["brand"] = cleanQuery($_GET["brand"]);    
        $data["brand"] = ($data["brand"]=="EMPTY") ? "" : $data["brand"];
        $data["model"] = cleanQuery($_GET["model"]);    
        $data["model"] = ($data["model"]=="EMPTY") ? "" : $data["model"];
        $data["w"] = cleanQuery($_GET["w"]);    
        $data["h"] = cleanQuery($_GET["h"]);    
        $data["midp"] = cleanQuery($_GET["midp"]);    
        $data["create_date"] = time();
        
        $appRegisterId = ApplicationRegister($data);
        
        if($appRegisterId > 0){
            $output["status"] = true;   
            $output["res"] = "S#" . $appRegisterId . "#E";   
        } else {
            $output["status"] = false;
            $output["res"] = "";    
        }        
    } else {
        $output["status"] = false;
        $output["res"] = "";
    }

    $log_output = "Status: " . $output["status"] . ", res: " . $output["res"];
    $str = " Transaction at " . date("d/m/Y H:i:s") . " \n Input: " . $log_input . " \n " . "Output: " . $log_output . "\n\n";

    $location = getcwd() . "/log/" . date("Y") . "/" . date("md") . "/";                                       
    if(@chdir($location) == false) mkdir($location, 0777, true);
    
    $file = $location . "log_application_register.log";
    $conf = fopen($file, 'a') or die("can't open file");        
    fwrite($conf, $str);        
    fclose($conf);
    
    echo json_encode($output["res"]);
?>
